/* Generate and display a calendar to handle the date input type */

Calendar=Class.create();
Calendar.define(
);

CalendarView=Class.create({
    /**
     * corresponding words for days and month
     */
  day_of_week: Array('L','M','M','J','V','S','D'),
      months:Array('Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Decembre')
  });
CalendarView.define(
		    
);

var interval=5;
var selectIndex=5;
var CurrentYear=0;
var CurrentMonth=1;
var CurrentCount=0;
var toGo="dateSelected";
var mn=['Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Decembre'];
var dim=[31,0,31,30,31,30,31,31,30,31,30,31];

function updateDate(updateOptions,day,month,year){

  val=0;
  dayBegin=0;
  dayEnd=0;
  monthBegin=0;
  monthEnd=0;
  yearEnd=0;
  if(day==0 && month==0 && year==0){
    document.getElementById(toGo).value="0";
    document.getElementById(toGo+"Bottom").value="0";
    document.getElementById(toGo+"Top").value="0";
  } 
  else{
    switch(updateOptions){
    case 0:
      val=day+"/"+month+"/"+year;
      dayBegin=((day-interval)>1) ? (day-interval) : 1;
      dayEnd=((day+interval)<dim[month-1]) ? (day+interval) : ((day+interval)-dim[month-1]) ;
      yearEnd=year;
      monthEnd=month;
      break;
    case 1:
      val=day+"/"+month+"/"+year;
      dayBegin=(day>1) ? day : 1;
      dayEnd=((day+7)<dim[month-1]) ? (day+7) : dim[month-1] ;
      yearEnd=year;
      monthEnd=month;
      break;
    case 2:
      val=day+"/"+month+"/"+year;
      dayBegin=(day>1) ? day : 1;
      dayEnd=dim[month-1] ;
      yearEnd=year;
      monthEnd=month;
      break;
    case 3:
      val=day+"/"+month+"/"+year;
      dayBegin=(day>1) ? day : 1;
      dayEnd=(day<dim[month-1]) ? day : dim[month-1] ;
      monthEnd=(month+6 <= 12) ? month+7 : (12-month)+6;
      yearEnd=(month+6 <= 12) ? year : year+1;
      break;
    case 4:
      val=day+"/"+month+"/"+year;
      dayBegin=(day>1) ? day : 1;
      dayEnd=(day<dim[month-1]) ? day : dim[month-1] ;
      monthEnd=month;
      yearEnd=year+1;
      break;
    }

    document.getElementById(toGo).value=year+"/"+month+"/"+day;
    document.getElementById(toGo+"Bottom").value=year+"/"+month+"/"+dayBegin;
    document.getElementById(toGo+"Top").value=yearEnd+"/"+monthEnd+"/"+dayEnd;
    document.getElementById("calendarspace").style.display='none';
  }

  //alert(document.getElementById(toGo+"Bottom").value+" -> "+document.getElementById(toGo+"Top").value);
  if(updateOptions==0){

    /**
     * Create and option element with item's id and val 
     */
    oOption=document.createElement('option');
    try{
      /** Mozilla */
      oOption.setAttribute('value',val);
      oOption.appendChild(document.createTextNode(val));
    }
    catch(e){
      /** IE */
      oOption.value = val;
      oOption.text = val;
    }
    
    tmp=document.getElementById('date')
      try{
	tmp.options[selectIndex]=oOption;
      }
      catch(e){
	try{
	  tmp.appendChild(oOption);
	}
	catch(e){
	  alert("impossible d'utiliser la table d'éléments d'un select !! \n");
	}
      }

    lim=tmp.options.length-1;
    //alert(lim);
    tmp.focus();
    tmp.selectedIndex=lim;

  }
  else{

    if(document.getElementById('date').options.length>selectIndex){
      for(i=selectIndex;i<=document.getElementById('date').options.length;i++){
	document.getElementById('date').remove(i);
      }
    }
      
  }

  if(__sys_debug)
    document.getElementById("console").innerHTML+=toGo+" => "+document.getElementById(toGo).value+" "+toGo+"Top => "+document.getElementById(toGo+"Top").value+" "+toGo+"Bottom => "+document.getElementById(toGo+"Bottom").value;

  search();
}

function setReturn(v){
toGo=v;
}

function displayCal(m, y, cM, cH, cDW, cD, brdr){
  CurrentYear = y;
  CurrentMonth = m;

  var oD = new Date(y, m-1, 1); //to fix date bug when current day is 31st
  oD.od=oD.getDay()+1; //to fix date bug when current day is 31st
    

  var todaydate=new Date();
  var scanfortoday=(y==todaydate.getFullYear() && m==todaydate.getMonth()+1)? todaydate.getDate() : 0;

    dim[1]=(((oD.getFullYear()%100!=0)&&(oD.getFullYear()%4==0))||(oD.getFullYear()%400==0))?29:28;

  var t='<div><table class="'+cM+'" cellpadding="0" cellspacing="0" width="184"><tr align="center">';

  t+='<td colspan="7" align="center" class="'+cH+'"><a class="calleft" href=\"javascript: calendarUp('+(-1)+')\" alt="" title=""> &laquo; </a>'+mn[m-1]+' - '+y+'<a class="calright" href=\"javascript: calendarUp('+(1)+')\" alt="" title=""> &raquo; </a></td></tr><tr bgcolor="#8EB6BE" align="center">';

  for(s=0;s<7;s++)t+='<td width="26" class="'+cDW+'">'+
		    "LMMJVSD".substr(s,1)+
		    '</td>';

  t+='</tr><tr class="calendar_date" align="center">';
  if(__sys_debug)
    document.getElementById('console').innerHTML="";
  
  firstPassed=false;
  limit=0;
  mustHaveLessRow=false;
  for(i=0;i<=(42-limit);i++){
    if(mustHaveLessRow && i+6==42)
      break;
    x=((i-oD.od>=-1) && (i-oD.od<dim[m-1]-1))? i-oD.od+2 : '&nbsp;';    
    day=x;

    if(!firstPassed){
      if(i==0 && (i-oD.od>=-1)){ // le premier jour est un dimanche ?
	for(j=0;j<7;j++)
	  if(j+1==7 || j+2==7 )
	    if(j+1==7)
	      t+='<td class="free_day"><a href=\"javascript: updateDate(0,'+x+','+CurrentMonth+','+CurrentYear+');\" >'+x+'</a></td>';
	    else
	      t+='<td class="free_day">&nbsp;</td>';
	  else
	    t+='<td class="'+cD+'">'+(j+1)+'</td>';
	t+='</tr><tr class="calendar_date" align="center">';
	mustHaveLessRow=true;
      }
      i++;
      x=((i-oD.od>=-1) && (i-oD.od<dim[m-1]-1))? i-oD.od+2 : '&nbsp;';    
      day=x;
      firstPassed=true;
    }

    if(__sys_debug)
      document.getElementById('console').innerHTML+="i-oD.od : "+(i-oD.od)+". i-oD.od : "+(i-oD.od+2)+". dim[m-1] : "+dim[m-1]+". m : "+m+"<br/> \n" ;

    if ((x>0 || x.length>0) && x!='&nbsp;')
      x='<a href=\"javascript: updateDate(0,'+x+','+CurrentMonth+','+CurrentYear+');\" >'+x+'</a>';

    if(day==scanfortoday)
	t+='<td style="background-color:#C0D1D4;">'+x+'</td>';
    else{
      if(((((i+1)%7==0)&&(i+1<36)) || (((i)%7==0)&&(i<36))) || i==41 || i==42)
	t+='<td class="jour_ferie">'+x+'</td>';
      else
	t+='<td class="'+cD+'">'+x+'</td>';
    }

    if(((i)%7==0)&&(i<36))
      t+='</tr><tr class="calendar_date" align="center">';

  }
  return t+='</tr></table></div>';
}

function displayMoreMonth(cM, cH, cDW, cD, brdr){
  if(CurrentMonth+1<=12){
      CurrentMonth+=1;
  }
  else{
    CurrentMonth=1;
    CurrentYear+=1;
  }
  return displayCal(CurrentMonth, CurrentYear, cM, cH, cDW, cD, brdr);
}

function displayLessMonth(cM, cH, cDW, cD, brdr){
  if(CurrentMonth-1>0)
    CurrentMonth-=1;
  else{
    CurrentMonth=12;
    CurrentYear-=1;
  }
  return displayCal(CurrentMonth, CurrentYear, cM, cH, cDW, cD, brdr);
}

function calendarUp(newyear){
  var calendarstr=(newyear > 0) ?
    displayMoreMonth("calendar", "calendar_top", "daysofweek", "days", 0) :
    displayLessMonth("calendar", "calendar_top", "daysofweek", "days", 0) ;
  if (document.getElementById)
    document.getElementById("calendarspace").innerHTML=calendarstr
}

/* */

function CalendarInitialisation(){
  var todaydate=new Date();
  var month=todaydate.getMonth()+1; //get current month (1-12)
  var year=todaydate.getFullYear(); //get current year
  //parseGet();
  document.getElementById('calendarspace').innerHTML=
    new CalendarView(month,year);
    //    displayCal(curmonth, curyear, "calendar", "calendar_top", "daysofweek", "days", 0);
}
